// Interrupts.c

#include "system.h"

INTERRUPTHANDLERS interrupts;

void initInterrupts(void)
{
	//Set Pins 23,24,44 as interrupts
	_CN6IE  = 1;
	_CN7IE  = 1;
	//_CN22IE = 1;
	_CNIP0 = 1;
	_CNIP1 = 0;
	_CNIP2 = 1;
 	_CNIF = 0;
 	_CNIE = 1;
}

void __attribute__((interrupt,no_auto_psv)) _CNInterrupt()
{
	if(!IR1I)
		interrupts.ir1Interrupt  = 1;
	if(!IR2I)
		interrupts.ir2Interrupt  = 1;
	if(RFIDI)
		interrupts.rfidInterrupt = 1;
	_CNIF = 0; //Clear CN interrupt flag
}

//Low Level System Error Interrupts
void __attribute__((interrupt, no_auto_psv)) _OscillatorFail(void)
{
	int i,b;
	INTCON1bits.OSCFAIL = 0;        //Clear the trap flag
	while(1)
	{
		LED4 = 1;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 0;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 1;
	}
}

void __attribute__((interrupt, no_auto_psv)) _AddressError(void)
{
	int i,b;
    INTCON1bits.ADDRERR = 0;        //Clear the trap flag
	while(1)
	{
		LED4 = 1;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 0;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 1;
	}
}
void __attribute__((interrupt, no_auto_psv)) _StackError(void)
{
	int i,b;
    INTCON1bits.STKERR = 0;         //Clear the trap flag
	while(1)
	{
		LED4 = 1;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 0;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 1;
	}
}

void __attribute__((interrupt, no_auto_psv)) _MathError(void)
{
	int i,b;
    INTCON1bits.MATHERR = 0;        //Clear the trap flag
	while(1)
	{
		LED4 = 1;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 0;
		
		for(i=0;i<10000;i++)
		{
			for(b = 0; b<1;b++)
			{
			}
		}

		LED4 = 1;
	}
}

